<?php

require_once('../../config/maptrick.conf.php');
require_once(SITE_ROOT . 'class/db/DBCountry.class.php');

$iso3166 = array();
$doc = new DOMDocument();
$doc->load('iso3166-1.xml');

$codes = $doc->getElementsByTagName('ISO_3166-1_Entry');

foreach ($codes as $code) {
	$names = $code->getElementsByTagName('ISO_3166-1_Country_name');
	$name = $names->item(0)->nodeValue;
	
	$elements = $code->getElementsByTagName('ISO_3166-1_Alpha-2_Code_element');
	$element = $elements->item(0)->nodeValue;
	
	$iso3166[strtolower($name)] = $element;
}

print_r($iso3166);

$db = new DBCountry();
$countries = $db->getCountries();

foreach ($countries as $country) {
	switch ($country->getCountryID()) {
		case 2: // England
			$country->setISO3166("GB");
			break;
		case 93: // Northern Ireland
			$country->setISO3166("GB");
			break;
		case 26: // Scotland
			$country->setISO3166("GB");
			break;
		case 61: // Wales
			$country->setISO3166("GB");
			break;
		case 136: // Brunei
			$country->setISO3166("BN");
			break;
		case 126: // C™te dÕIvoire
			$country->setISO3166("CI");
			break;
		case 60: // Chinese Taipei
			$country->setISO3166("TW");
			break;
		case 97: // FYR Macedonia
			$country->setISO3166("MK");
			break;
		case 85: // Iran
			$country->setISO3166("IR");
			break;
		case 103: // Moldova
			$country->setISO3166("MD");
			break;
		case 15: // Oceania
			$country->setISO3166("OC");
			break;
		case 34: // People's Republic of China
			$country->setISO3166("CN");
			break;
		case 35: // Russia
			$country->setISO3166("RU");
			break;
		case 30: // South Korea
			$country->setISO3166("KR");
			break;
		case 140: // Syria
			$country->setISO3166("SY");
			break;
		case 142: // Tanzania
			$country->setISO3166("TZ");
			break;
		case 110: // Trinidad & Tobago
			$country->setISO3166("TT");
			break;
		case 8: // USA
			$country->setISO3166("US");
			break;
		case 70: // Vietnam
			$country->setISO3166("VN");
			break;
		default:
			$country->setISO3166($iso3166[strtolower($country->getEnglishName())]);
			break;
	}
}

print_r($countries);

$db->updateISO3166($countries);

?>